package com.shop.dao.impl;

import com.shop.dao.ShopDao;
import com.shop.entity.ShopCat;
import com.shop.entity.ShowPageT;
import com.shop.tools.JdbcTool;

import java.sql.SQLException;
import java.util.List;

/**
 * com.shop.dao.impl文档说明
 *
 * @author Angluzha
 * @version 2022/9/21
 */
public class ShopCatDaoImpl implements ShopDao {

    @Override
    public ShopCat selectOne(ShopCat shopCat) throws SQLException {
        String sql = "select * from shopcat where id=?";
        return JdbcTool.selectOne(ShopCat.class,sql,shopCat.getId());
    }

    @Override
    public boolean update(ShopCat shopCat) {
        String sql = "update shopcat set pid=?,uid=?,quantity=? where id =?";
        return JdbcTool.write(sql,shopCat.getPid(),shopCat.getUid(),shopCat.getQuantity(),shopCat.getId())>0;
    }

    @Override
    public boolean insert(ShopCat shopCat) {
        String sql = "insert into shopcat(uid,pid,quantity) values(?,?,?)";
        return JdbcTool.write(sql,shopCat.getUid(),shopCat.getPid(),shopCat.getQuantity())>0;
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql = "delete from shopcat where id=?";
        return JdbcTool.write(sql,id)>0;
    }

    @Override
    public List<ShopCat> selectAll() throws SQLException {
        String sql = "select * from shopcat";
        return JdbcTool.selectList(ShopCat.class,sql);
    }

    @Override
    public ShowPageT<ShopCat> selectPage(ShowPageT<ShopCat> page) throws SQLException {
        String sql = "SELECT shopcat.*,goods.gname,goods.des,goods.price,goods.pnguri FROM shopcat INNER JOIN goods ON shopcat.pid = goods.id where uid = ? LIMIT ?,?";
        String sql2 = "select count(1) from shopcat where uid = ?";
        Integer uid = page.getUid();
        page.setCount(JdbcTool.selectResult(Integer.class,sql2,uid));
        page.setData(JdbcTool.selectList(ShopCat.class,sql,uid,page.getSize()*(page.getPage()-1),page.getSize()));
        return page;
    }

    @Override
    public int delCheck(List<Integer> list) {
        StringBuffer sql = new StringBuffer("delete from shopcat where id in(");
        list.forEach(e->sql.append("?,"));
        sql.setCharAt(sql.length()-1,')');
        return JdbcTool.write(String.valueOf(sql),list.toArray());
    }

    @Override
    public Integer selectCountByUid(Integer id) throws SQLException {
        String sql2 = "select count(1) from shopcat where uid = ?";
        return JdbcTool.selectResult(Integer.class,sql2,id);
    }

    @Override
    public boolean revise(Integer id, Integer status) {
        String sql = "update shopcat set quantity = quantity+? where id=?";
        return JdbcTool.write(sql,status,id)>0;
    }

    @Override
    public ShopCat exist(ShopCat shopCat) throws SQLException {
        String sql = "select * from shopcat where pid=? and uid=?";
        return JdbcTool.selectOne(ShopCat.class,sql,shopCat.getPid(),shopCat.getUid());
    }

    @Override
    public void addQuantity(ShopCat shopCat) {
        String sql = "update shopcat set quantity =? where id = ?";
        JdbcTool.write(sql,shopCat.getQuantity(),shopCat.getId());
    }
}
